package itbour.onetouchshow.bean.inching.noppt;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.bean.canvas.PosBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.FineTuneRender;

/**
 * Created by zjl_d on 2017/11/24.
 */

public class InchingOutLineTableBean extends InchingOutLineAllTypeBase {


    private int rowCnt;
    private int colCnt;
    private ArrayList<String> ds;
    private PosBean pos;
    private List<InchingOutLineTextOrShapeBean> children;

    public int getRowCnt() {
        return rowCnt;
    }

    public void setRowCnt(int rowCnt) {
        this.rowCnt = rowCnt;
    }

    public int getColCnt() {
        return colCnt;
    }

    public void setColCnt(int colCnt) {
        this.colCnt = colCnt;
    }

    public ArrayList<String> getDs() {
        return ds;
    }

    public void setDs(ArrayList<String> ds) {
        this.ds = ds;
    }

    public PosBean getPos() {
        return pos;
    }

    public void setPos(PosBean pos) {
        this.pos = pos;
    }

    public List<InchingOutLineTextOrShapeBean> getChildren() {
        return children;
    }

    public void setChildren(List<InchingOutLineTextOrShapeBean> children) {
        this.children = children;
    }


    public ArrayList<ArrayList<String>> getTableObjectDataSource() {
        if (ct != AppConst.INCHING_TABLEVIEW_TYPE) {
            return null;
        }
        if (children == null || children.size() <= 0) {
            return null;
        }

        ArrayList<InchingOutLineTextOrShapeBean> cells = new ArrayList<>();
        for (int i = 0; i < children.size(); i++) {
            InchingOutLineTextOrShapeBean c = children.get(i);
            if (c.getCt() != AppConst.INCHING_TEXTVIEW_TYPE) {
                continue;
            }
            cells.add(c);
        }

        ArrayList<ArrayList<String>> rowArr = new ArrayList(rowCnt);
        for (int i = 0; i < cells.size(); i++) {
            int row = i / colCnt;
            ArrayList<String> colArr = null;
            if (rowArr.size() <= row) {
                colArr = new ArrayList<>(colCnt);
                rowArr.add(colArr);
            } else {
                colArr = rowArr.get(row);
            }
            String te = cells.get(i).getTe();
            if (te == null) {
                te = "";
            }
            colArr.add(te);
        }
        return rowArr;
    }


    public void cacheTableDataSource(ArrayList<ArrayList<String>> data, int index) {
        // 记录要更新的数据源
        ds = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            ArrayList<String> tmp = data.get(i);
            if (i == 0) {
                colCnt = tmp.size(); // 记录列数
            }
            ds.addAll(tmp);
        }
        rowCnt = data.size(); // 记录行数
        updateToFineTune(index);
    }

    public void updateToFineTune(int index) {
        FineTuneRender.changeTableTextForJava(index, id, getRowCnt(), getColCnt(), new Gson().toJson(ds));
    }


}
